<template>
  <div>
    <SearchForm
      @search="onSearch"
      @reset="onReset"
      :search-list="searchList" 
      :search-params="searchParams">
    </SearchForm>

    <div class="table-layout">
      <el-card shadow="never">
        <div class="table-box">
          <el-table
            size="large"
            stripe
            border
            v-loading="loading"
            max-height="500px"
            :data="tableData"
            row-key="id"
            element-loading-spinner="el-icon-loading"
            element-loading-background="rgba(255, 255, 255, 0.8)"
            @selection-change="onSelect"
          >
            <el-table-column prop="nickname" label="用户昵称" width="150"/>
            <el-table-column prop="avatar" label="头像" width="100">
              <template slot-scope="scope">
                <el-avatar :size="40" :src="scope.row.avatar"></el-avatar>
              </template>
            </el-table-column>
            <el-table-column prop="gender" label="性别" width="80">
              <template slot-scope="scope">
                {{ scope.row.gender === 1 ? '男' : scope.row.gender === 2 ? '女' : '未知' }}
              </template>
            </el-table-column>
            <el-table-column prop="phone" label="手机号" width="120"/>
            <el-table-column prop="city" label="城市" width="120"/>
            <el-table-column prop="province" label="省份" width="120"/>
            <el-table-column prop="country" label="国家" width="120"/>
            <el-table-column prop="status" label="状态" width="100">
              <template slot-scope="scope">
                <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'">
                  {{ scope.row.status === 1 ? '正常' : '已封禁' }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="created_at" label="注册时间" width="180"/>
            <el-table-column prop="last_login_time" label="最后登录时间" width="180"/>
            <el-table-column label="操作" fixed="right" width="150">
              <template slot-scope="scope">
                <el-button 
                  v-if="scope.row.status === 1" 
                  type="text" 
                  style="color: #F82222" 
                  @click="handleBlock(scope.row)"
                >封禁</el-button>
                <el-button 
                  v-else 
                  type="text" 
                  @click="handleUnblock(scope.row)"
                >解封</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-card>
      <div class="page-box">
        <el-pagination
          background
          :current-page="pageNo"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalSize"
          @size-change="changePageSize"
          @current-change="changePageNo"
        />
      </div>
    </div>
  </div>
</template>

<script>
import crud from "@/mixins/crud";
import { http } from "@/api";
import SearchForm from "@/components/SearchForm";

export default {
  name: "MiniprogramUsers",
  components: {
    SearchForm
  },
  mixins: [crud],
  data() {
    return {
      request: http.miniProgramUsers,
      searchList: [
        {
          label: '用户昵称',
          prop: 'nickname',
          name: 'input',
          clearable: true
        },
        {
          label: '手机号',
          prop: 'phone',
          name: 'input',
          clearable: true
        },
        {
          label: '状态',
          prop: 'status',
          name: 'select',
          clearable: true,
          options: [
            { label: '正常', value: 1 },
            { label: '已封禁', value: 0 }
          ]
        }
      ],
      searchParams: {
        nickname: '',
        phone: '',
        status: ''
      },
      defaultParams: {}
    }
  },
  methods: {
    handleBlock(row) {
      this.$confirm('确认封禁该用户吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.block(row.id).then(() => {
          this.$message.success('封禁成功')
          this.initData()
        })
      }).catch(() => {
        this.$message.info('已取消操作')
      })
    },
    handleUnblock(row) {
      this.$confirm('确认解封该用户吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.request.unblock(row.id).then(() => {
          this.$message.success('解封成功')
          this.initData()
        })
      }).catch(() => {
        this.$message.info('已取消操作')
      })
    }
  }
}
</script>

<style scoped lang="scss">
.table-layout {
  margin-top: 20px;
}
.table-handle {
  margin-bottom: 20px;
}
.page-box {
  margin-top: 20px;
  text-align: right;
}
</style>
